import { Controller, Get, Post, Body, Patch, Param, Delete, Put, Req } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/index';
import { UpdateUserDto } from './dto/index';
import { ResultData } from 'src/common/utils/result';
import { Request } from 'express';
import { UserBindEnterpriseDto, UserBindRoleDto } from './dto';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('/getUserInfo')
  async getUserInfo(@Req() req: Request) {
    const user = req.user as { userId: number };
    const result = await this.userService.getUserInfo(user.userId);
    const { password, ...userInfo } = result;
    return ResultData.ok(userInfo);
  }

  @Get('/getUserInfo/:id')
  async getUserInfoById(@Param('id') userId: number) {
    const result = await this.userService.getUserInfo(userId);
    return ResultData.ok(result);
  }

  @Post('bindRole')
  async userBindRole(@Body() data: UserBindRoleDto, @Req() req: Request) {
    const user = req.user as { userId: number };
    const res = await this.userService.userBindRole({
      userId: user.userId,
      roleIds: data.roleIds,
    });
    return ResultData.ok(res);
  }

  @Post('bindEnterprise')
  async userBindEnterprise(@Body() data: UserBindEnterpriseDto, @Req() req: Request) {
    const user = req.user as { userId: number };
    const result = await this.userService.userBindEnterprise({
      userId: user.userId,
      enterpriseId: data.enterpriseId,
    });
    return ResultData.ok(result);
  }

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get()
  async findAll() {
    const res = await this.userService.findAll();
    return ResultData.ok(res);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
